﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Xml;
using OPMS_V1.Public;
using OPMS_V1.DOL;
using System.Collections.Generic;

// THIS PAGE IS COMPLETED YET, WE ARE EXPECTING THE NEXT GROUP TO COMPLETE IT! 

namespace OPMS_V1.Public
{
    public partial class search : System.Web.UI.Page
    {
        S_PDataContext dc = new S_PDataContext();

        public void Page_Load(object sender, EventArgs e)
        {
            String area = Request.QueryString["area"];
            String propertyID = Request.QueryString["property"];
            String numBedRoom = Request.QueryString["numBedRoom"];
            String minPrice = Request.QueryString["minPrice"];
            String maxPrice = Request.QueryString["maxPrice"];
            string[] tempArray;
            

            pnlPropDetail.Visible = false;
            lblNoSearch.Visible = true;
            

            // this will display all the properties provided area is not left blank!
            if (area != "" && propertyID == null)
            {
                DataTable dt = new DataTable();                

                dt.Columns.Add(new DataColumn("propertyID", typeof(string)));
                dt.Columns.Add(new DataColumn("pathImg01", typeof(string)));
                dt.Columns.Add(new DataColumn("address", typeof(string)));
                dt.Columns.Add(new DataColumn("price", typeof(string)));
                dt.Columns.Add(new DataColumn("selectedArea", typeof(string)));
                dt.Columns.Add(new DataColumn("area", typeof(string)));
                dt.Columns.Add(new DataColumn("heading", typeof(string)));
                dt.Columns.Add(new DataColumn("description", typeof(string)));

                pnlPropDetail.Visible = false;
                


                // this is for Testing pruposes ...
                if (minPrice == "")
                    minPrice = "1";
                if (maxPrice == "")
                    maxPrice = "99999999999";
                if (numBedRoom == "")
                    numBedRoom = "0";

                tempArray = area.Split(',');

                for (int i = 0; i < tempArray.Length; i++)
                {
                    // getAllProperties will make Use of LINQ to fetch all property details and return the DataTable 'dt'  accordingly
                    getAllProperties(tempArray[i].Trim(), minPrice.Trim(), maxPrice.Trim(), numBedRoom.Trim(), dt);
                    
                }

                DataView dv = new DataView(dt);
                listProp.DataSource = dv;
                listProp.DataBind();
                if (!(dv.Count == 0))
                    lblNoSearch.Visible = false;
                
            }
            else if(propertyID != "")
            {
                lblNoSearch.Visible = false; 
                showPropertyByPropertyID(propertyID);
            }   
        }

        // This Function will make Use of LINQ to fetch all property details and return the DataTable 'dt'  accordingly
        public void getAllProperties(string area,string minPrice, string maxprice, string numBedRoom, DataTable dt)
        {
            DataRow dr;

            var properties = dc.Get_properties_by_areaDetails(area, minPrice, maxprice, numBedRoom);
            foreach (var property in properties)
            {
                String propID = null;
                String pathImg01 = null;
                String address = null;
                String price = null;
                String Area = null;
                String heading = null;
                String description = null;

                propID = property.propId.ToString();
                pathImg01 = property.pathImg1.ToString();
                if (!System.IO.File.Exists(Server.MapPath(pathImg01))) pathImg01 = "images/no-photo.jpg";
                address = property.streetNum.ToString() + " " +
                          property.streetName.ToString() + ", " +
                          property.area.ToString().ToUpper() + "  " +
                          property.postalCode.ToString();
                price = "Rs. " + property.cost + ".00";
                Area = property.area.ToString() + ", " + property.state.ToString().ToUpper();
                heading = property.PropertyHeading.ToString();
                description = property.PropertyDescription.ToString();

                dr = dt.NewRow();

                dr[0] = propID;
                dr[1] = pathImg01;
                dr[2] = address;
                dr[3] = price;
                dr[4] = area;
                dr[5] = Area;
                dr[6] = heading;
                dr[7] = description;

                dt.Rows.Add(dr);
            }
        }

        // Thsi funtion will be responsble to hide and display the appropiate panels and will then Provide a 
        // detailed description about a property by its ID
        public void showPropertyByPropertyID(string propID)
        {
            String prevPageURL = "";
            if(Request.UrlReferrer != null)
               prevPageURL = Request.UrlReferrer.ToString();
           
            var temparray = dc.Get_property_by_propertyID(propID);
            foreach (var prop in temparray)
            {
                String address = prop.streetNum.ToString() + " " +
                          prop.streetName.ToString() + ", " +
                          prop.area.ToString().ToUpper() + "  " +
                          prop.postalCode.ToString();

                //if the image doesn't exist then replace it with a Custom 'mo-photo.jpg'
                string imgpath1 = prop.image01.ToString();
                    if (!System.IO.File.Exists(Server.MapPath(imgpath1))) imgpath1 = "images/no-photo.jpg";
                string imgpath2 = prop.image02.ToString();
                    if (!System.IO.File.Exists(Server.MapPath(imgpath2))) imgpath2 = "images/no-photo.jpg";
                string imgpath3 = prop.image03.ToString();
                    if (!System.IO.File.Exists(Server.MapPath(imgpath3))) imgpath3 = "images/no-photo.jpg";
                string imgpath4 = prop.image04.ToString();
                    if (!System.IO.File.Exists(Server.MapPath(imgpath4))) imgpath4 = "images/no-photo.jpg";
                string imgpath5 = prop.image05.ToString();
                    if (!System.IO.File.Exists(Server.MapPath(imgpath5))) imgpath5 = "images/no-photo.jpg";

                A1.HRef = imgpath1;
                imgP1.ImageUrl = imgpath1;
                imgP1.ToolTip = address;
                A2.HRef = imgpath2;
                imgP2.ImageUrl = imgpath2;
                imgP2.ToolTip = address;
                A3.HRef = imgpath3;
                imgP3.ImageUrl = imgpath3;
                imgP3.ToolTip = address;
                A4.HRef = imgpath4;
                imgP4.ImageUrl = imgpath4;
                imgP4.ToolTip = address;
                A5.HRef = imgpath5;

                lblPropType0.Text = prop.PropertyType.ToString();
                lblSuburb0.Text = prop.area.ToString();
                lblDwellings0.Text = prop.Dwelling.ToString();
                lblHouseSize0.Text = prop.HouseSize.ToString();
                //lblYearBuilt0.Text = prop.yearofBuilt.ToString();
                lblAddress.Text = address;
                lblCost0.Text = prop.cost.ToString();
                //LblBond0.Text = prop.Bond.ToString();
                lblName0.Text = prop.ContactFName.ToString() + " " + prop.ContactLName.ToString();
                lblphone0.Text = prop.Phone.ToString();
                lblEmail0.Text = prop.Email.ToString();


            }
            backBtn.HRef = prevPageURL;
            pnlPropDetail.Visible = true;
        }
    }
}
